package com.example.javaproject.thread.thread;

import java.util.concurrent.locks.LockSupport;

public class StopThreadTest {
    private volatile boolean isRun = true;

    private Thread t2 = new Thread(() -> {
        try {
            while (isRun) {
                Thread.sleep(2);
                log(Thread.currentThread().getName() + " isRunning");
                LockSupport.park();
            }
            log(Thread.currentThread().getName() + " end run");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }, "t2");
    private Thread t1 = new Thread(() -> {
        try {
            while (isRun) {
                Thread.sleep(2);
                log(Thread.currentThread().getName() + " isRunning");
            }
            log(Thread.currentThread().getName() + " end run");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }, "t1");

    private static void log(String msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) throws InterruptedException {
//        test1();
//        test2();
//        test3();
//        test4();
        test5();
    }

    private static void test1() throws InterruptedException {
        StopThreadTest test = new StopThreadTest();
        test.t1.start();
        Thread.sleep(10);
        test.isRun = false;
        Thread.sleep(10);
        log(test.t1.getName() + " isAlive = " + test.t1.isAlive());
    }

    private static void test2() throws InterruptedException {
        StopThreadTest test = new StopThreadTest();
        test.t2.start();
        Thread.sleep(10);
        test.isRun = false;
        log(test.t1.getName() + " isAlive = " + test.t1.isAlive());
    }

    private Thread t3 = new Thread(() -> {
        try {
            // 检查中断标记
            while (!Thread.currentThread().isInterrupted()) {
                log(Thread.currentThread().getName() + " isRunning");
            }
            log(Thread.currentThread().getName() + " end run");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }, "t3");

    private static void test3() throws InterruptedException {
        StopThreadTest test = new StopThreadTest();
        test.t3.start();
        Thread.sleep(10);
        test.t3.interrupt();
        log(test.t3.getName() + " isInterrupt = " + test.t3.isInterrupted());
        log(test.t3.getName() + " isAlive = " + test.t3.isAlive());
    }



    private Thread t4 = new Thread(() -> {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(5);
                log(Thread.currentThread().getName() + " isRunning");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log(Thread.currentThread().getName() + " end run");
    }, "t4");

    private static void test4() throws InterruptedException {
        StopThreadTest test = new StopThreadTest();
        test.t4.start();
        Thread.sleep(10);
        test.t4.interrupt();
        log(test.t4.getName() + " isInterrupt = " + test.t4.isInterrupted());
        log(test.t4.getName() + " isAlive = " + test.t4.isAlive());
    }

    private Thread t5 = new Thread(() -> {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(5);
                log(Thread.currentThread().getName() + " isRunning");
            } catch (Exception e) {
                if (e instanceof InterruptedException) {
                    Thread.currentThread().interrupt();
                }
            }
        }
        log(Thread.currentThread().getName() + " end run");
    }, "t5");

    private static void test5() throws InterruptedException {
        StopThreadTest test = new StopThreadTest();
        test.t5.start();
        Thread.sleep(10);
        test.t5.interrupt();
        Thread.sleep(100);
        log(test.t5.getName() + " isInterrupt = " + test.t5.isInterrupted());
        log(test.t5.getName() + " isAlive = " + test.t5.isAlive());
    }

}
